home *** CD-ROM | disk | FTP | other *** search
- unit LogStrm;
-
- interface
-
- uses
- SysUtils, Classes, Forms;
-
- type
- TLogFile = class
- private
- logFileName: string;
- stream: TFileStream;
- function GetLogFileName: string;
- public
- constructor Create;
- destructor Destroy;
- procedure WriteToStream(strToWrite: string; blankLine: boolean);
- end;
-
- implementation
-
- constructor TLogFile.Create;
- begin
- logFileName := GetLogFileName;
- stream := TFileStream.Create(logFileName, fmCreate);
- end;
-
- destructor TLogFile.Destroy;
- begin
- stream.Free;
- end;
-
- procedure TLogFile.WriteToStream(strToWrite: string; blankLine: boolean);
- var
- strNew: string;
- lengthStrNew: byte absolute strNew;
- begin
- { #13 = carriage return #10 = form feed }
- strNew := Format('%s%s', [strToWrite, #13#10]);
- if blankLine then
- strNew := Format('%s%s', [strNew, #13#10]);
- stream.Write(strNew[1], lengthStrNew);
- end;
-
- function TLogFile.GetLogFileName: string;
- const
- logFileName = 'log';
- var
- i: integer;
- logFileNoExt: string;
- begin
- logFileNoExt := Concat(ExtractFilePath(Application.ExeName),
- logFileName);
- i := 1;
- repeat
- Result := Format('%s%d.txt', [logFileNoExt, i]);
- Inc(i);
- until not FileExists(Result);
- end;
-
- end.
-
- { example ... }
- procedure WriteToLogFile;
- const
- testDir = 'test';
- var
- dirToMake: string;
- begin
- with TLogFile.Create do
- try
- WriteToStream(Format('Log begun at %s', [DateTimeToStr(Now)]),True);
- dirToMake := Concat(ExtractFilePath(Application.ExeName), testDir);
- if not DirectoryExists(dirToMake) then begin
- MkDir(dirToMake);
- if IOResult = 0 then
- WriteToStream(Format('Made dir %s', [dirToMake]), False)
- else
- WriteToStream(Format('Unable to make dir %s', [dirToMake]), False);
- end else
- WriteToStream(Format('Directory %s already exists', [dirToMake]), False);
- finally
- Destroy;
- end;
- end;